package com.cincc.cinccimdemo.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.TimeUtils;
import com.cincc.cinccimdemo.common.callback.OnConversationItemClickListener;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.model.MessageInfoUI;
import com.cincc.cinccimdemo.ui.conversation.ConversationActivity;
import com.cincc.im.model.dbmodel.ConversationInfo;
import com.cincc.im.model.enums.ImSendType;
import com.cincc.im.model.enums.MessageType;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ConversationListAdapter extends RecyclerView.Adapter<ConversationListAdapter.MyViewHolder> {

    private static final String TAG = "ConversationListAdapter";
    Context mContext;
    List<ConversationInfo> list = new ArrayList<>();

    public ConversationListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setConversationList(List<ConversationInfo> conversationInfos) {
        this.list.clear();
        list.addAll(conversationInfos);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.conversationlist_item_conversation, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ConversationInfo item = list.get(position);
        Glide.with(mContext).load(item.protraitUrl).placeholder(ImSendType.isP2P(item.type) ? R.mipmap.avatar_def : R.mipmap.ic_group_chat)
                .transforms(new CenterCrop(), new RoundedCorners(15))
                .into(holder.portraitImageView);
        holder.contentTextView.setText(getShowContent(item));
        holder.nameTextView.setText(ImSendType.isP2P(item.type) ? item.userName : item.groupName);
        holder.slient.setVisibility(item.noDisturb ? View.VISIBLE : View.GONE);
        holder.unreadCountTextView.setVisibility(item.unreadNum > 0 ? View.VISIBLE : View.GONE);
        holder.unreadCountTextView.setText(String.valueOf(item.unreadNum));
        holder.itemView.setBackgroundResource(item.isTop ? R.drawable.selector_stick_top_item : R.drawable.selector_common_item);
        holder.timeTextView.setText(TimeUtils.getMsgFormatTime(item.updateTime));
        holder.promptTextView.setText(!TextUtils.isEmpty(item.draft) ? "[草稿]" : "");

        holder.itemView.setOnClickListener(view -> startConversation(item));
        holder.itemView.setOnLongClickListener(view -> {
            onItemClickListener.onItemLongClick(view, position, item);
            return false;
        });
    }


    private void startConversation(ConversationInfo conversationInfo) {
        ConversationActivity.startConversation(mContext, conversationInfo);
    }

    public void deleteConversation(ConversationInfo conversationInfo) {
        int index = -1;
        for (int i = 0; i < this.list.size(); i++) {
            ConversationInfo uiMessage = list.get(i);
            if (uiMessage.id == conversationInfo.id) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            notifyItemRemoved(index);
        }
    }

    public void setConversationRead(ConversationInfo conversationInfo, int num) {
        int index = -1;
        for (int i = 0; i < this.list.size(); i++) {
            ConversationInfo uiMessage = list.get(i);
            if (uiMessage.id == conversationInfo.id) {
                index = i;
                break;
            }
        }
        if (index > -1) {
            list.get(index).unreadNum = num;
            notifyItemChanged(index);
        }
    }

    private String getShowContent(ConversationInfo item) {
        if (item.isRevoke) {
            return "消息已撤回";
        }
        if (item.messageType == MessageType.IMAGE) {
            return "[图片]";
        } else if (item.messageType == MessageType.VOICE) {
            return "[语音]";
        } else if (item.messageType == MessageType.VIDEO) {
            return "[视频]";
        } else if (item.messageType == MessageType.FILE) {
            return "[文件]";
        } else if (item.messageType == MessageType.LOCATION) {
            return "[位置]";
        }
        String content = "";
        try {
            JSONObject j = new JSONObject(item.messageContent);
            content = j.getString("msgContent");
        } catch (Exception e) {
            Log.e(TAG, "getShowContent: " + e.getMessage());
        }
        return content;
    }


    public OnConversationItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnConversationItemClickListener itemClickListener) {
        this.onItemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView portraitImageView;
        TextView unreadCountTextView;
        View redDotView;
        TextView nameTextView;
        TextView timeTextView;
        //提示文本 比如有人@我或者草稿
        TextView promptTextView;
        ImageView statusImageView;
        TextView contentTextView;
        ImageView slient;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            portraitImageView = itemView.findViewById(R.id.portraitImageView);
            unreadCountTextView = itemView.findViewById(R.id.unreadCountTextView);
            redDotView = itemView.findViewById(R.id.redDotView);
            nameTextView = itemView.findViewById(R.id.nameTextView);
            timeTextView = itemView.findViewById(R.id.timeTextView);
            promptTextView = itemView.findViewById(R.id.promptTextView);
            statusImageView = itemView.findViewById(R.id.statusImageView);
            contentTextView = itemView.findViewById(R.id.contentTextView);
            slient = itemView.findViewById(R.id.slient);
        }
    }
}
